<?php

if($noview) { return; }

echo $html->div("statistics");
echo '<h2>'.__('Team Season Statistics', true).'</h2>';

$paginator->options(array('url' => $this->passedArgs));
$options = array('url' => array(
	'sort' => null,
	'direction' => null,
	'page' => empty($this->passedArgs['page']) ? 1 : $this->passedArgs['page']
));

$fields = array(
	'Team' => array(
		'strName' => array(
			'title' => __('Team', true),
			'description' => __('Team name', true),
			'direction' => 'asc'),
		'iValue' => array(
			'title' => __('Tv', true),
			'description' => __('Team Value', true),
			'direction' => 'desc')),
	'Coach' => array(
		'strName' => array(
			'title' => __('Coach', true),
			'description' => __('Coach name', true),
			'direction' => 'none')),
	'Race' => array(
		'ID' => array(
			'title' => __('Race', true),
			'description' => __('Race', true),
			'direction' => 'none')),
	'StatisticsSeasonTeam' => array(
		'iMatch' => array(
			'title' => __('G', true),
			'description' => __('Game', true),
			'direction' => 'desc'),
		'iMatchPlayed' => array(
			'title' => __('Gp', true),
			'description' => __('Played Game', true),
			'direction' => 'desc'),
		'iWins' => array(
			'title' => __('W', true),
			'description' => __('Wins', true),
			'direction' => 'desc'),
		'iDraws' => array(
			'title' => __('D', true),
			'description' => __('Draws', true),
			'direction' => 'desc'),
		'iLoss' => array(
			'title' => __('L', true),
			'description' => __('Loss', true),
			'direction' => 'desc'),
		'fPerWins' => array(
			'title' => __('%W', true),
			'description' => __('% of Wins', true),
			'direction' => 'desc'),
		'fPerDraws' => array(
			'title' => __('%D', true),
			'description' => __('% of Draws', true),
			'direction' => 'desc'),
		'fPerLoss' => array(
			'title' => __('%L', true),
			'description' => __('% of Loss', true),
			'direction' => 'desc'),

		'Inflicted_iPasses' => array(
			'title' => __('Pa', true),
			'description' => __('Inflicted Passes', true),
			'direction' => 'desc'),
		'Inflicted_iCatches' => array(
			'title' => __('Ca', true),
			'description' => __('Inflicted Catches', true),
			'direction' => 'desc'),
		'Inflicted_iInterceptions' => array(
			'title' => __('Int', true),
			'description' => __('Inflicted Interceptions', true),
			'direction' => 'desc'),
		'Inflicted_iTouchdowns' => array(
			'title' => __('TD', true),
			'description' => __('Inflicted Touchdowns', true),
			'direction' => 'desc'),
		'Inflicted_iCasualties' => array(
			'title' => __('Cas', true),
			'description' => __('Inflicted Casualties', true),
			'direction' => 'desc'),
		'Inflicted_iTackles' => array(
			'title' => __('Tac', true),
			'description' => __('Inflicted Tackles', true),
			'direction' => 'desc'),
		'Inflicted_iKO' => array(
			'title' => __('KO', true),
			'description' => __('Inflicted KO', true),
			'direction' => 'desc'),
		'Inflicted_iInjuries' => array(
			'title' => __('Inj', true),
			'description' => __('Inflicted Injuries', true),
			'direction' => 'desc'),
		'Inflicted_iDead' => array(
			'title' => __('Dd', true),
			'description' => __('Inflicted Dead', true),
			'direction' => 'desc'),
		'Inflicted_iMetersRunning' => array(
			'title' => __('MR', true),
			'description' => __('Inflicted Meters Running', true),
			'direction' => 'desc'),
		'Inflicted_iMetersPassing' => array(
			'title' => __('MP', true),
			'description' => __('Inflicted Meters Passing', true),
			'direction' => 'desc'),
			'Sustained_iPasses' => array(
			'title' => __('sPa', true),
			'description' => __('Sustained Passes', true),
			'direction' => 'desc'),
		'Sustained_iCatches' => array(
			'title' => __('sCa', true),
			'description' => __('Sustained Catches', true),
			'direction' => 'desc'),
		'Sustained_iInterceptions' => array(
			'title' => __('sInt', true),
			'description' => __('Sustained Interceptions', true),
			'direction' => 'desc'),
		'Sustained_iTouchdowns' => array(
			'title' => __('sTD', true),
			'description' => __('Sustained Touchdowns', true),
			'direction' => 'desc'),
		'Sustained_iCasualties' => array(
			'title' => __('sCas', true),
			'description' => __('Sustained Casualties', true),
			'direction' => 'desc'),
		'Sustained_iTackles' => array(
			'title' => __('sTac', true),
			'description' => __('sTac', true),
			'direction' => 'desc'),
		'Sustained_iKO' => array(
			'title' => __('sKO', true),
			'description' => __('Sustained KO', true),
			'direction' => 'desc'),
		'Sustained_iInjuries' => array(
			'title' => __('sInj', true),
			'description' => __('Player', true), 'direction' => 'desc'),
		'Sustained_iDead' => array(
			'title' => __('sDd', true),
			'description' => __('Sustained Dead', true), 'direction' => 'desc'),
		'Sustained_iMetersRunning' => array(
			'title' => __('sMR', true),
			'description' => __('Sustained Meters Running', true), 'direction' => 'desc'),
		'Sustained_iMetersPassing' => array(
			'title' => __('sMP', true),
			'description' => __('Sustained Meters Passing', true), 'direction' => 'desc'),
/*
		'iBestMatchRating' => array(
			'title' => __('BMR', true),
			'description' => __('Best Match Rating', true),
			'direction' => 'desc'),*/
		'Average_iMatchRating' => array(
			'title' => __('AvMR', true),
			'description' => __('Average Match Rating', true),
			'direction' => 'desc'),
/*		'Average_iSpectators' => array(
			'title' => __('AvSp', true),
			'description' => __('Average Spectators', true),
			'direction' => 'desc'),*/
		'Average_iCashEarned' => array(
			'title' => __('AvCE', true),
			'description' => __('Average Cash Earned', true),
			'direction' => 'desc'),
/*		'iSpectators' => array(
			'title' => __('Sp', true),
			'description' => __('Spectators', true),
			'direction' => 'desc'),*/
		'iCashEarned' => array(
			'title' => __('CE', true),
			'description' => __('Cash Earned', true),
			'direction' => 'desc'),
/*		'iPossessionBall' => array(
			'title' => __('PB', true),
			'description' => __('Possession Ball', true),
			'direction' => 'desc'),
		'Occupation_iOwn' => array(
			'title' => __('OccO', true),
			'description' => __('Occupation Own', true),
			'direction' => 'desc'),
		'Occupation_iTheir' => array(
			'title' => __('OccT', true),
			'description' => __('Occupation Their', true),
			'direction' => 'desc'),*/
	)
);


$current = empty($this->passedArgs['sort'])?'StatisticsSeasonTeam.iWins':$this->passedArgs['sort'];
$cur = split('\.', $current);
$fields["$cur[0]"]["$cur[1]"]['direction'] = (empty($this->passedArgs['direction']) || $this->passedArgs['direction'] == 'desc')?'asc' : 'desc';

echo '<p>';
echo $paginator->counter(array(
	'format' => __('Page %page% of %pages%, showing %current% records out of %count% total, starting on record %start%, ending on %end%', true)
));
echo '</p>';

echo '<table cellpadding="0" cellspacing="0">';
echo '<tr>';
echo '<th>Rank</th>';
foreach ( $fields as $key => $value ) {
	foreach( $fields[$key] as $subkey => $subvalue ) {

		$options['url']['sort'] = $key.'.'.$subkey;
		$options['url']['direction'] = $fields[$key][$subkey]['direction'];

		echo '<th><acronym title="'.$fields[$key][$subkey]['description'].'">';
		if( $fields[$key][$subkey]['direction'] != 'none') {
			echo $paginator->sort($fields[$key][$subkey]['title'], $key.'.'.$subkey, $options);
		} else {
			echo $fields[$key][$subkey]['title'];
		}
		echo '</acronym></th>';

	}
}
echo '</tr>';

$i = 0;
foreach ($statisticsSeasonTeams as $statisticsSeasonTeam) {
	$class = null;
	if((!empty($statisticsSeasonTeam['Team']['Coach']))
			&& $statisticsSeasonTeam['Team']['Coach']['ID'] == $coach_id) {
		$class = ' class="mine';
	} else {
		$class = ' class="';
	}
	if ($i++ % 2 == 0) {
		$class .= ' altrow"';
	} else {
		$class .= '"';
	}

	echo '<tr '.$class.'>';
	echo '<td nowrap>'.$i.'</td>';
	echo '<td nowrap>'.$html->link($statisticsSeasonTeam['Team']['strName'], array('controller' => 'teams', 'action' => 'view', $statisticsSeasonTeam['Team']['ID'])).'</td>';
	echo '<td>'.$statisticsSeasonTeam['Team']['iValue'].'</td>';
	if(!empty($statisticsSeasonTeam['Team']['Coach'])) {
		echo '<td nowrap>'.$html->link($statisticsSeasonTeam['Team']['Coach']['strName'], array('controller' => 'coaches', 'action' => 'view', $statisticsSeasonTeam['Team']['Coach']['ID'])).'</td>';
	} else {
		echo '<td></td>';
	}
	echo '<td nowrap>'.$html->link($statisticsSeasonTeam['Team']['Race']['LocalizedString'][$locale], array('controller' => 'races', 'action' => 'view', $statisticsSeasonTeam['Team']['Race']['ID'])).'</td>';

	foreach ( $fields['StatisticsSeasonTeam'] as $key => $value ) {
		switch ( $key ) {
			default:

				if(ereg("fAvg", $key)) {
					$value = $number->precision($statisticsSeasonTeam['StatisticsSeasonTeam'][$key],1);
				} else if (ereg("fPer", $key)) {
					$value = $number->toPercentage($statisticsSeasonTeam['StatisticsSeasonTeam'][$key],1);
				} else {
					$value = $statisticsSeasonTeam['StatisticsSeasonTeam'][$key];
				}
				echo '<td>'.$value.'</td>';
				break;
		}
	}
	echo '</tr>';
}
echo '</table>';
echo $html->div("paginating");
echo $paginator->prev('<< '.__('previous', true), array(), null, array('class'=>'disabled'));
echo ' | '.$paginator->numbers();
echo $paginator->next(__('next', true).' >>', array(), null, array('class' => 'disabled'));
echo '</div>';
echo '</div>';
?>